package com.neuedu.spring.controller;

import com.neuedu.spring.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;

/**
 * 项目：spring-java4
 * 创建时间：  2022-03-21   8:43
 * 作者 :jshand
 * 描述 :
 */
//@Component
@Controller("uController")
public class UserController implements BeanNameAware , ApplicationContextAware , InitializingBean , DisposableBean {
    @Autowired
    private UserService userService;

    public void query(){
        System.out.println("调用Controller方法");
        userService.queryById();
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("获取名字: "+s);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println(applicationContext.hashCode());
        System.out.println(applicationContext);


        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println("beanDefinitionName = " + beanDefinitionName);
        }

    }

    /**
     * 对象被创建成功了
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("uController 对象创建完成");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("容器销毁的时候执行");
    }
}
